#include "Main/stdafx.h"
#include "BJLGameScence.h"
#include "BJLPlayer.h"
#include "Game/GameLib.h"
#include "CMD_BJLOx.h"
#include "BJLPlayer.h"

class BJLTouchScence
	:public cocos2d::Layer
{
public:
	virtual bool init()
	{
		if ( !cocos2d::Layer::init() )
		{
			return false;
		}
		setTouchEnabled(true);
		setTouchMode(cocos2d::Touch::DispatchMode::ONE_BY_ONE);
		return true;
	}
public:
	CREATE_FUNC(BJLTouchScence);
public:
	virtual bool onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent)
	{
		if (!utility::isAllVisible(this))
		{
			return false;
		}
		if (pTouch->getID() != 0)
		{
			return false;
		}
		return BJLGameScence::Instance().ccTouchBegan(pTouch->getLocation());
	}
};

void BJLGameScence::initTouch()
{
	BJLTouchScence* pTouchScence = new BJLTouchScence;
	pTouchScence->init();
	addChild(pTouchScence);
}

bool BJLGameScence::ccTouchBegan(cocos2d::Vec2 kPos)
{
	if (!isVisible())
	{
		return false;
	}
	//CCLOG("+++++++++++++++++++(%f,%f)",kPos.x,kPos.y);
	if (groupLayout->isVisible())
	{
		groupLayout->setVisible(false);
		btn_group->setVisible(true);
	}

	if (!panel_betAreas->isVisible() || !panel_bottom->isVisible() || !(getBJLGameState() == BJL_STATE_BETSTART))
	{
		return false;
	}

	if (!isCanClickBetArea)  //
		return false;
	
	if (p_betAreas[AREA_XIAN_DUI]->getBoundingBox().containsPoint(kPos))
	{
		CCLOG("AREA_XIAN_DUI");
		if (m_myBetAreaTotal[AREA_XIAN_DUI] + m_chipValue > BETAREA_MYSCORE_MAX)
		{
			NoticeMsg::Instance().ShowTopMsgByScript("BetAreaScore_Max");
			return false;
		}

		isCanClickBetArea = false;

		CMD_C_PlaceBet scoreInfo;
		scoreInfo.cbBetArea = AREA_XIAN_DUI;
		scoreInfo.lBetScore = m_chipValue;
		SendSocketData(SUB_C_PLACE_JETTON, &scoreInfo, sizeof(scoreInfo));
		log("====================1==>send score:%lld", scoreInfo.lBetScore);
		showChipInAni(AREA_XIAN_DUI);
	}
	else if (p_betAreas[AREA_ZHUANG_DUI]->getBoundingBox().containsPoint(kPos))
	{
		CCLOG("AREA_ZHUANG_DUI");
		if (m_myBetAreaTotal[AREA_ZHUANG_DUI] + m_chipValue > BETAREA_MYSCORE_MAX)
		{
			NoticeMsg::Instance().ShowTopMsgByScript("BetAreaScore_Max");
			return false;
		}

		isCanClickBetArea = false;

		CMD_C_PlaceBet scoreInfo;
		scoreInfo.cbBetArea = AREA_ZHUANG_DUI;
		scoreInfo.lBetScore = m_chipValue;
		SendSocketData(SUB_C_PLACE_JETTON, &scoreInfo, sizeof(scoreInfo));
		log("====================1==>send score:%lld", scoreInfo.lBetScore);
		showChipInAni(AREA_ZHUANG_DUI);
	}
	else if (p_betAreas[AREA_XIAN]->getBoundingBox().containsPoint(kPos))
	{
		CCLOG("AREA_XIAN");
		if (m_myBetAreaTotal[AREA_XIAN] + m_chipValue > BETAREA_MYSCORE_MAX)
		{
			NoticeMsg::Instance().ShowTopMsgByScript("BetAreaScore_Max");
			return false;
		}

		isCanClickBetArea = false;

		CMD_C_PlaceBet scoreInfo;
		scoreInfo.cbBetArea = AREA_XIAN;
		scoreInfo.lBetScore = m_chipValue;
		SendSocketData(SUB_C_PLACE_JETTON, &scoreInfo, sizeof(scoreInfo));
		log("====================1==>send score:%lld", scoreInfo.lBetScore);
		showChipInAni(AREA_XIAN);
	}
	else if (p_betAreas[AREA_PING]->getBoundingBox().containsPoint(kPos))
	{
		CCLOG("AREA_PING");
		if (m_myBetAreaTotal[AREA_PING] + m_chipValue > BETAREA_MYSCORE_MAX)
		{
			NoticeMsg::Instance().ShowTopMsgByScript("BetAreaScore_Max");
			return false;
		}

		isCanClickBetArea = false;

		CMD_C_PlaceBet scoreInfo;
		scoreInfo.cbBetArea = AREA_PING;
		scoreInfo.lBetScore = m_chipValue;
		SendSocketData(SUB_C_PLACE_JETTON, &scoreInfo, sizeof(scoreInfo));
		log("====================1==>send score:%lld", scoreInfo.lBetScore);
		showChipInAni(AREA_PING);
	}
	else if (p_betAreas[AREA_ZHUANG]->getBoundingBox().containsPoint(kPos))
	{
		CCLOG("AREA_ZHUANG");
		if (m_myBetAreaTotal[AREA_ZHUANG] + m_chipValue > BETAREA_MYSCORE_MAX)
		{
			NoticeMsg::Instance().ShowTopMsgByScript("BetAreaScore_Max");
			return false;
		}

		isCanClickBetArea = false;

		CMD_C_PlaceBet scoreInfo;
		scoreInfo.cbBetArea = AREA_ZHUANG;
		scoreInfo.lBetScore = m_chipValue;
		SendSocketData(SUB_C_PLACE_JETTON, &scoreInfo, sizeof(scoreInfo));
		log("====================1==>send score:%lld", scoreInfo.lBetScore);
		showChipInAni(AREA_ZHUANG);
	}
	else if (p_betAreas[AREA_XIAN_TIAN]->getBoundingBox().containsPoint(kPos))
	{
		CCLOG("AREA_XIAN_TIAN");
		if (m_myBetAreaTotal[AREA_XIAN_TIAN] + m_chipValue > BETAREA_MYSCORE_MAX)
		{
			NoticeMsg::Instance().ShowTopMsgByScript("BetAreaScore_Max");
			return false;
		}

		isCanClickBetArea = false;

		CMD_C_PlaceBet scoreInfo;
		scoreInfo.cbBetArea = AREA_XIAN_TIAN;
		scoreInfo.lBetScore = m_chipValue;
		SendSocketData(SUB_C_PLACE_JETTON, &scoreInfo, sizeof(scoreInfo));
		log("====================1==>send score:%lld", scoreInfo.lBetScore);
		showChipInAni(AREA_XIAN_TIAN);
	}
	else if (p_betAreas[AREA_TONG_DUI]->getBoundingBox().containsPoint(kPos))
	{
		CCLOG("AREA_TONG_DUI");
		if (m_myBetAreaTotal[AREA_TONG_DUI] + m_chipValue > BETAREA_MYSCORE_MAX)
		{
			NoticeMsg::Instance().ShowTopMsgByScript("BetAreaScore_Max");
			return false;
		}

		isCanClickBetArea = false;

		CMD_C_PlaceBet scoreInfo;
		scoreInfo.cbBetArea = AREA_TONG_DUI;
		scoreInfo.lBetScore = m_chipValue;
		SendSocketData(SUB_C_PLACE_JETTON, &scoreInfo, sizeof(scoreInfo));
		log("====================1==>send score:%lld", scoreInfo.lBetScore);
		showChipInAni(AREA_TONG_DUI);
	}
	else if (p_betAreas[AREA_ZHUANG_TIAN]->getBoundingBox().containsPoint(kPos))
	{
		CCLOG("AREA_ZHUANG_TIAN");
		if (m_myBetAreaTotal[AREA_ZHUANG_TIAN] + m_chipValue > BETAREA_MYSCORE_MAX)
		{
			NoticeMsg::Instance().ShowTopMsgByScript("BetAreaScore_Max");
			return false;
		}

		isCanClickBetArea = false;

		CMD_C_PlaceBet scoreInfo;
		scoreInfo.cbBetArea = AREA_ZHUANG_TIAN;
		scoreInfo.lBetScore = m_chipValue;
		SendSocketData(SUB_C_PLACE_JETTON, &scoreInfo, sizeof(scoreInfo));
		log("====================1==>send score:%lld", scoreInfo.lBetScore);
		showChipInAni(AREA_ZHUANG_TIAN);
	}

	return false;
}